﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using NHibernateDB;
using System.Threading;

namespace Grafic
{
    public partial class GraficLumina : Form
    {

        public DateTime dataInceput;
        public int idsenzor;
       
        ZedGraphControl zg;
        PointPairList list;
        DateTime dataIneput;
        LineItem myCurve;
        GraphPane myPane;
        GraphPane myPaneEmpty;
        double diff = 0;

        string CNP;
        string Nume;
        string Prenume;
        List<string> namesForm;
        public GraficLumina(string CNP, string Nume, string Prenume,string limba)
        {
            InitializeComponent();

            string xmlFile = @"C:\Language" + limba + ".xml";


            XMLParser3 xmlParser = new XMLParser3(xmlFile);
            namesForm = xmlParser.ExtractAttribute("GraficLumina", string.Empty, string.Empty, "name");

            this.Text = namesForm[0] + Nume + " " + Prenume;
            this.CNP = CNP;
            this.Nume = Nume;
            this.Prenume = Prenume;

            this.zg = zg1;
            list = new PointPairList();
            myPane = zg.GraphPane;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public void CreateGraph()
        {
            //myPane = zg.GraphPane;

            // Set the titles and axis labels

            //PersistenceManager pm = PersistenceManager.Instance;
            //IList<DateGeneralePacient> listdate = pm.RetrieveEquals<DateGeneralePacient>("IdPacient", CNP);

            //myPane.Title.Text = "Grafic Temperatura: " + listdate[0].Nume + " " + listdate[0].Prenume;
            myPane.XAxis.Title.Text = namesForm[1] + dataInceput.ToString();
            myPane.YAxis.Title.Text = namesForm[3];
            myPane.Title.Text = namesForm[0] + Nume + " " + Prenume;
            // Fill the area under the curve with a white-red gradient at 45 degrees
            //myCurve.Line.Fill = new Fill(Color.White, Color.Red, 45F);
            // Make the symbols opaque by filling them with white
            //myCurve.Symbol.Fill = new Fill(Color.White);

            // Fill the axis background with a color gradient
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);

            // Fill the pane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);

            // Calculate the Axis Scale Ranges
            zg.AxisChange();
            Ref(this);
        }

        private delegate void RefDelegate(GraficLumina oControl);

        private void Ref(GraficLumina oControl)
        {
            if (oControl.InvokeRequired)
            {
                // This is a worker thread so delegate the task.


                RefDelegate d = new RefDelegate(Ref);
                oControl.Invoke(d, new object[] { oControl });

            }
            else
            {
                oControl.Refresh();
            }
        }

        public void AddValue(string value, string stringdate)
        {

            double val = double.Parse(value);
            DateTime data = DateTime.Parse(stringdate);

            if (list.Count == 0)
            {
                myPaneEmpty = myPane;
                list.Add(0, val);
                dataIneput = data;
            }
            else
            {
                zg1 = new ZedGraphControl();
                myPane = zg1.GraphPane;
                //myPane = myPaneEmpty;
                TimeSpan ts = data - dataIneput;
                double differeneInseconds = ts.TotalSeconds;
                diff += differeneInseconds;

                list.Add(differeneInseconds, val);
            }

            myCurve = myPane.AddCurve(namesForm[0], list, Color.Blue,
                                    SymbolType.Circle);

            myCurve.Line.Fill = new Fill(Color.White, Color.Red, 45F);
            // Make the symbols opaque by filling them with white
            myCurve.Symbol.Fill = new Fill(Color.White);

            zg.AxisChange();
            Ref(this);

        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            SetSize();
        }

        private void SetSize()
        {
            zg1.Location = new Point(10, 10);
            // Leave a small margin around the outside of the control
            zg1.Size = new Size(this.ClientRectangle.Width - 20, this.ClientRectangle.Height - 20);
        }

    }
}